package com.pwc.testing;

import java.io.File;
import java.io.IOException;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.pwc.dataAdaptor.ExcelUtil;
import com.pwc.dataAdaptor.IDataAdaptor;

@Component
public class TestingProcess implements ITestingProcess {

	@Autowired
	private ExcelUtil util;

	@Autowired
	private Selenium selenium;

	@Autowired
	private LogUtil logUtil;
	
	
	private List<IDataAdaptor> excelDataList;

	public void initial(String driverPath, String filePath, String browser, String logPath) {

		File excelPath = new File(filePath);
		try {

			logUtil.setLogPath(logPath);

			logUtil.setLogName(excelPath.getName());

			logUtil.addLog("begin to process:" + excelPath.getName()+"\n");
		} catch (Exception e) {
			e.printStackTrace();
			return;
		}

		try {
			util.initial(excelPath);
			selenium.initial(driverPath, browser,logUtil);
		} catch (IOException e) {
			logUtil.addLog("IO error, please path is right.\n");
			logUtil.addLog("detial:"+e.getMessage());
		}catch (Exception e) {
			logUtil.addLog("error in initial.\n");
			logUtil.addLog("detial:"+e.getMessage()+"\n");
			return;
		}

	}

	public String process() {
		
		//
		excelDataList=util.getDataSet();
//		
//		// do pre process
		preProcess preProcess=new preProcess(excelDataList);
		
		excelDataList= preProcess.mainPreprocess();
		

		// run first sheet
		for (IDataAdaptor dataAdaptor : excelDataList) {
			logUtil.addLog("run task:" + dataAdaptor.getName()+"\n");

			// doing process
			for (String[] command : dataAdaptor.getAllData()) {
				try {
					singleCommand(command);
				} catch (Exception e) {
					logUtil.addLog("error in log.\n");
					logUtil.addLog("detial:"+e.getMessage()+"\n");
				}

			}
		}

		return null;
	}

	/**
	 * 
	 * @param c
	 * @throws NumberFormatException
	 * @throws InterruptedException
	 */
	private void singleCommand(String[] c) throws NumberFormatException, InterruptedException {
		if (c.length > 0) {
			String command = c[0];
			if (c.length == 2 && Command.GET.equalsIgnoreCase(command)) {
				selenium.getSystem().get(c[1]);
			} else if (Command.QUIT.equalsIgnoreCase(command)) {
				selenium.getSystem().quit();
			} else if (c.length == 4 && Command.INPUT.equalsIgnoreCase(command)) {
				selenium.getAction(). input(c[1], c[2], c[3]);
			} else if (c.length == 3 && Command.CLICK.equalsIgnoreCase(command)) {
				selenium.getAction().click(c[1], c[2]);
			} else if(c.length == 3 && Command.CLEAR.equalsIgnoreCase(command)){
				selenium.getAction().clear(c[1], c[2]);
			}else if (c.length == 2 && Command.SLEEP.equalsIgnoreCase(command)) {
				selenium.getSystem().sleep(Long.parseLong(c[1]));
			}else if (c.length == 3 && Command.DBCLICK.equalsIgnoreCase(command)) {
				selenium.getAction().dbClick (c[1], c[2]);
			} else if (Command.BACK.equalsIgnoreCase(command)) {
				selenium.getSystem().back();
			} else if (Command.FORWARD.equalsIgnoreCase(command)) {
				selenium.getSystem().forward();
			}else if (c.length > 4 &&Command.ASSERT_EQUALS.equalsIgnoreCase(command)) {
				selenium.getAssert().assertEquals(c);
			}else if (c.length > 4 &&Command.ASSERT_CONTAINS.equalsIgnoreCase(command)) {
				selenium.getAssert().assertContains(c);
			}else if (c.length > 4 &&Command.ASSERT_GREATER.equalsIgnoreCase(command)) {
				selenium.getAssert().assertGreater(c);
			}else if (c.length > 4 &&Command.ASSERT_LESS.equalsIgnoreCase(command)) {
				selenium.getAssert().assertLess(c);
			}else if(c.length == 2 &&Command.SWITCH_WINDOW.equalsIgnoreCase(command)){
				selenium.getSystem().switchToWindow(c[1]);
			}  else if(c.length == 4 &&Command.SELECT_BY_VISIBILE_TEXT.equalsIgnoreCase(command)){
				selenium.getAction().selectByVisibleText(c[1], c[2], c[3]);
			}else if(c.length == 4 &&Command.SELECT_BY_INDEX.equalsIgnoreCase(command)){
				selenium.getAction().selectByIndex(c[1], c[2], c[3]);
			}else if(c.length == 5 &&Command.MOVE_OVER.equalsIgnoreCase(command)){
				selenium.getAction().moveOver(c[1], c[2], c[3],c[4]);
			}		
			else{
				logUtil.addLog("command error:"+ StringUtils.join(c, ','));
			}

		}

	}

}
